package com.zhihuichongcheng.application.ui.task.adapter.holder;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.smartlib.cmnObject.util.DateUtil;
import com.smartlib.cmnObject.util.DialogUtil;
import com.smartlib.cmnObject.util.ToastOpt;
import com.zhihuichongcheng.application.R;
import com.zhihuichongcheng.application.base.BaseActivity;
import com.zhihuichongcheng.application.bean.task.TaskMsgBean;
import com.zhihuichongcheng.application.constant.NetworkAPI;
import com.zhihuichongcheng.application.net.HttpCallback;
import com.zhihuichongcheng.application.ui.task.TaskDetailActivity;

import java.util.Date;
import java.util.HashMap;

/**
 * TaskMsgHolder Created by LK on 2017/1/6.
 */
public class TaskMsgHolder extends BaseHolder {
    private View itemLayout;
    private TextView tvTitle, tvTime;
    private TextView tvContent;
    private ImageView imgIcon;

    public TaskMsgHolder(View itemView, BaseActivity activity) {
        super(itemView, activity);
        itemLayout = itemView.findViewById(R.id.item_task_msg);
        tvTitle = (TextView) itemView.findViewById(R.id.tv_title);
        tvTime = (TextView) itemView.findViewById(R.id.tv_date);
        tvContent = (TextView) itemView.findViewById(R.id.tv_content);
        imgIcon = (ImageView) itemView.findViewById(R.id.img_notice);
    }

    public void loadData(final TaskMsgBean taskMsgBean) {
        tvTitle.setText(taskMsgBean.getMissionName());
        tvContent.setText(taskMsgBean.getContent());
        Date date = DateUtil.parseStringToDate(taskMsgBean.getCreateTime(), "yyyy年MM月dd日 HH:mm:ss");
        tvTime.setText(DateUtil.formatDateToString(date, "MM-dd HH:mm"));
        imgIcon.setVisibility(taskMsgBean.getState() == 1 ? View.VISIBLE : View.GONE);

        itemLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                lookedMsg(taskMsgBean);
            }
        });
    }

    /**
     * 消息设置已读t
     */
    private void lookedMsg(final TaskMsgBean taskMsgBean) {
        DialogUtil.showLoadDiadlog(getContext());
        HashMap<String, String> paramsDemo = new HashMap<>();
        paramsDemo.put("token", mUser.getToken());
        paramsDemo.put("messageIds", taskMsgBean.getId() + "");
        //筛选条件
        manager.post(NetworkAPI.MISSION_MSG_OPEN, new HttpCallback() {
            @Override
            public void onSuccess(int code, String value, String msg) {
                if (code == 2) {
                    ToastOpt.createToast(getContext(), msg);
                    return;
                }
                showTaskDetail(taskMsgBean);
            }

            @Override
            public void onFailure(String value) {
                showTaskDetail(taskMsgBean);
            }
        }, paramsDemo);
    }

    private void showTaskDetail(TaskMsgBean taskMsgBean) {
        DialogUtil.dismiss(getContext());
        Bundle bundle = new Bundle();
        bundle.putInt(TaskDetailActivity.EXTRA_TASK_ID, taskMsgBean.getTaskId());
        startActivity(TaskDetailActivity.class, bundle);
    }
}
